<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 22:26:12
 */
if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class MauthorContrl
{
    public function index()
    {
        global $_G;
        $pagesize = 30;
        if (!isset($_GET['authortype'])) {
            $counts = C::t('#jameson_read#jamesonread_authors')->count();
            $currentpage = $_GET['page']?intval($_GET['page']):1;
            $authorall = C::t('#jameson_read#jamesonread_authors')->fetch_all_author(($currentpage-1)*$pagesize,$pagesize);
            $pageshow = multi($counts,$pagesize,$currentpage,ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index');
        } else if ($_GET['authortype'] == 'shenhe') {
            $squsers = DB::fetch_all('SELECT * FROM %t WHERE status=%d ORDER BY addtime', array('jamesonread_shenhe', 0));
        } else if ($_GET['authortype'] == 'list') {
            $counts = DB::result_first('SELECT count(*) FROM %t WHERE status=%d', array('jamesonread_shenhe', 1));
            $currentpage = $_GET['page']?intval($_GET['page']):1;
            $squsers  = DB::fetch_all('SELECT * FROM %t WHERE status=%d ORDER BY addtime DESC LIMIT %d,%d', array('jamesonread_shenhe', 1, $pagesize*($currentpage-1),$pagesize));
            $pageshow = multi($counts,$pagesize,$currentpage,ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index&authortype=list');
        } else if ($_GET['authortype'] == 'fuli') {
            $fulitext = DB::fetch_first('SELECT topic_id,adv,type FROM %t WHERE type=%d', array('jamesonread_topics', 38));
        }
        include template('jameson_read:mauthor_index');
    }
    public function post()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            if ($_GET['status']) {
                C::t('#jameson_read#jamesonread_authors')->updateAll($_GET['status'], 'status');
            }
            if ($_GET['edit']) {
                C::t('#jameson_read#jamesonread_authors')->updateAll($_GET['edit'], 'edit');
            }
            if ($_GET['allowedit']) {
                C::t('#jameson_read#jamesonread_authors')->updateAll($_GET['allowedit'], 'allowedit');
            }
            if($_GET['setsj']){
                foreach ($_GET['setsj'] as $key => $field) {
                    $data = array();
                    $data['setsj'] = daddslashes($field);
                    $data[$data['setsj']] = intval($_GET[$data['setsj']][$key]);
                    C::t('#jameson_read#jamesonread_authors')->update($key,$data);
                }
            }
            cpmsg(lang('plugin/jameson_read', 'xiugaichenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index', 'error');
        }
    }
    public function shenhe()
    {
        global $_G;
        if (isset($_GET['authortype'])) {
            $forward = '&authortype=' . $_GET['authortype'];
        } else {
            $forward = '';
        }
        if (FORMHASH == $_GET['formhash']) {
            $nolang = lang('plugin/jameson_read', 'jujuetongguo') . '<a href="' . jamesonurl('author', 0, 'index') . '">' . lang('plugin/jameson_read', 'zuozhezhongxin') . '</a>';
            if (isset($_GET['del'])) {
                DB::delete('jamesonread_shenhe', "uid='" . $_GET['uid'] . "'", 1);
                if (('list' == $_GET['authortype']) && !in_array($_GET['uid'], $_G['jameson_read']['postbookusergroup'])) {
                    // 删除已通过审核中的作者，如果同时作者没在允许发布图书的用户组，则作者失去发布权限
                    $nolang = lang('plugin/jameson_read', 'beiquxiaoquanxian');
                } else if (!$_GET['authortype']) {
                    // 拒绝申请
                    $nolang = lang('plugin/jameson_read', 'jujueshenqing');
                }
                jameson_sendsms($_GET['uid'], 1, $_G['member']['username'], $nolang, 0);
                cpmsg(lang('plugin/jameson_read', 'caozuowancheng'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index&authortype=shenhe' . $forward, 'succeed');
            }
            DB::update('jamesonread_shenhe', array('status' => 1), DB::field('uid', $_GET['uid']));
            if (!DB::result_first('SELECT count(author_id) FROM %t WHERE author_id=%d', array('jamesonread_authors', intval($_GET['uid'])))) {
                C::t('#jameson_read#jamesonread_authors')->insert(array(
                    'author_id' => intval($_GET['uid']),
                    'status'    => 1,
                    'edit'      => 1,
                    'allowedit' => 1,
                ));
            } else {
                C::t('#jameson_read#jamesonread_authors')->update(intval($_GET['uid']), array(
                    'status'    => 1,
                    'edit'      => 1,
                    'allowedit' => 1,
                ));
            }
            jameson_sendsms($_GET['uid'], 1, $_G['member']['username'], lang('plugin/jameson_read', 'tongguoshenhe') . '<a href="' . jamesonurl('author', 0, 'index') . '">' . lang('plugin/jameson_read', 'zuozhezhongxin') . '</a>', 0);
            cpmsg(lang('plugin/jameson_read', 'caozuowancheng'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index&authortype=shenhe' . $forward, 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index&authortype=shenhe' . $forward, 'error');
        }
    }
    public function fuli()
    {
        global $_G;
        if (submitcheck('editsubmit') && isset($_GET['fulitext'])) {
            $text = daddslashes(trim($_GET['fulitext']));
            if (intval($_GET['fuliid'])) {
                $id = intval($_GET['fuliid']);
            } else {
                $id = DB::result_first('SELECT topic_id FROM %t WHERE type=%d', array('jamesonread_topics', 38));
            }
            C::t('#jameson_read#jamesonread_topics')->update($id, array('adv' => $text));
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index&authortype=fuli', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mauthor&act=index&authortype=fuli', 'error');
        }
    }
}